package org.tooyoung.fourone.live;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import org.tooyoung.fourone.BaseFragment;
import org.tooyoung.fourone.R;
import org.tooyoung.fourone.live.adapters.MyRoomListAdapter;
import org.tooyoung.fourone.live.clients.networkTool;
import org.tooyoung.fourone.live.models.Room;
import org.tooyoung.fourone.live.models.RoomData;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LiveFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {


    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private List<Room> list;
    private MyRoomListAdapter myRoomListAdapter;

    public LiveFragment() {
        // Required empty public constructor
    }

    @Override
    public String getTitle() {
        return "直播";
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_live, container, false);
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.live_refresh);
        recyclerView = (RecyclerView) view.findViewById(R.id.live_recyclerView);
        getRoomList();
        if (recyclerView != null) {
            refreshLayout.setOnRefreshListener(this);
        }
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
        recyclerView.setLayoutManager(layoutManager);


        return view;
    }

    void getRoomList() {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl("http://api.fenbei.com/");
        Retrofit retrofit = builder.addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
                .build();
        networkTool tool = retrofit.create(networkTool.class);
        Observable<RoomData> observable = tool.getRoomData(2, 1, 100000009, 1);
        observable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RoomData>() {
                    @Override
                    public void onCompleted() {
                        refreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(getContext(), "网络请求错误", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onNext(RoomData roomData) {
                        List<Room> rooms = roomData.getData().getRooms();
                        int size = rooms.size();
                        Log.d("sssss", "onNext: " + size);
                        for (int i = 0; i < size; i++) {
                            String headimg = rooms.get(i).getHeadimg();
                            Log.d("sssss", "onNext: " + headimg);
                        }
                        myRoomListAdapter = new MyRoomListAdapter(rooms, getContext());
                        recyclerView.setAdapter(myRoomListAdapter);
                        myRoomListAdapter.notifyDataSetChanged();
                    }
                });


    }

    @Override
    public void onRefresh() {
        getRoomList();
    }
}
